#include "mytime3.h"
Time::Time():hour(0),minute(0){

}

Time::Time(int h, int m):hour(h),minute(m)
{

}

Time::~Time()
{
//   std::cout << "Time destructed ..." << std::endl;
}

void Time::addHour(const int h)
{
    if (h < 0)
    {
        std::cout << "Can not add negative hours" << std::endl;
    }
    
    hour += h;
    adjustHourAndMinute(hour, minute);
}

void Time::addMinute(const int m)
{
    if (m < 0)
    {
        std::cout << "Can not add negative minutes" << std::endl;
        return;
    }
    
   minute += m;
   adjustHourAndMinute(hour, minute);
}

//const类型的方法（表明其内部不会改变this的状态）
//其内部调用的方法返回值也一定是const类型的
//https://stackoverflow.com/questions/56387509/this-argument-to-member-function-select-has-type-const-selectparam-but-fu
const Time Time::operator+(const Time & aTime)
{
    int sumHour = hour + aTime.hour;
    int sumMinute = minute + aTime.minute;
    // 如果是const类型的方法，那么这里会报错：
    // mytime3.cpp:43:5: error: 'this' argument to member function 'adjustHourAndMinute' has type 'const Time', but function is not marked const
    adjustHourAndMinute(sumHour, sumMinute);
    Time newTime(sumHour, sumMinute);
    return newTime;
}

const Time Time::operator-(const Time & aTime) {
    int hourResult = hour - aTime.hour;
    if (hourResult < 0)
    {
        using std::cout;
        using std::endl;
        cout << "The subtracted time is smaller then subtract time" << endl;
        return *this;
    }
    int mi = minute - aTime.minute;
    if (mi < 0)
    {
        mi = mi + 60;
        hourResult = hourResult - 1;
    }
    Time resultTime(hourResult, mi);
    return resultTime;
}

const Time Time::operator*(const int num) {
    int resultH = hour * num;
    int resultM = minute * num;
    adjustHourAndMinute(resultH, resultM);
    Time reusltTime(resultH, resultM);
    return reusltTime;
}
 
std::ostream & operator<<(std::ostream&s, Time & aTime) {
    using namespace std;
    s << "cout Hour: " << aTime.hour << " Minute: " << aTime.minute << endl;
    return s;
}

void Time::show() const
{
    using namespace std;
    cout << "Hour: " << hour << " Minute: " << minute << endl;
}

// 私有方法
void Time::adjustHourAndMinute(int &h, int &m)
{
    h = h + m / 60;
    h = h % 24;
    m = m % 60;
}
